//
//  TYFormData.m
//  TYUIKit_Example
//
//  Created by 谢航宇 on 2023/3/20.
//  Copyright © 2023 谢航宇. All rights reserved.
//

#import "TYFormData.h"

@implementation TYFormData
- (instancetype)initWithItemArray:(NSMutableArray *)itemArray {
    self = [super init];
    if (self) {
        self.itemArray = itemArray;
    }
    return self;
}

- (NSMutableDictionary *)requestParameters {
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    for (TYFormItem * item in self.itemArray) {
        if (item.key.length > 0) {
            [parameters setValue:[NSString stringWithFormat:@"%@",[item parameter]] forKey:item.key];
        }
    }
    return parameters;
}

@end


@implementation TYFormItem
- (instancetype)initWithType:(TYFormItemType)type {
    self = [super init];
    if (self) {
        self.type = type;
        
    }
    return self;
}


- (BOOL)isNew {
    if (self.currentValue == nil) {
        return NO;
    }
    
    if ([[NSString stringWithFormat:@"%@",self.currentValue]isEqualToString:[NSString stringWithFormat:@"%@",self.oldValue]]) {
        return NO;
    }
    
    return YES;
}

- (id)value {
    if (self.currentValue == nil) {
        return self.oldValue;
    }
    return self.currentValue;
}


- (NSString *)parameter {
    if ([self.value respondsToSelector:@selector(parameter)]) {
        return [self.value parameter];
    }else {
        return [self.value description];
    }
}
@end
